<?php

class Model_MenuBackend extends Model_Category {
	
	public function __construct(){
		parent::__construct();
		//去掉未授权的节点
		$cate = $this->_Cate;
		
		/* @var $cate Helper_Category */
		$data = $cate->getData();
		
		foreach ($data as $row){
			$privileges = explode(',', $row['privilege']);
			
			//判断是有权限访问该菜单
			$flag = empty($row['privilege']) ? true : false;

			foreach ($privileges as $privilege){
				if( UAC::isAuth($privilege) ) {
					$flag = true;
				}
			}
			
			$var_id = $cate->getConfig('VAR_ID');
			if ($flag == false) {
				$cate->removeNode($row[$var_id]);
			}
		}
		
	}
	
    /**
     *在读取每条数据后，生成默认的url 
     **/
	protected function _after_select_fetch(&$row){
		$row['_url'] = $row['url'];
		empty($row['url']) && $row['url'] = _url($row['ctl'],$row['action'],$row['parameter']);
	}
	
	
	/**
	 * 形成二级菜单
	 */
	function generateSubMenu(){
		$tree  = $this->getTree();
		$menu = array();
		$data = $this->getData();
		foreach ($tree as $aKey => $aTree){
			foreach ($aTree as $bKey => $bTree){
				if ("leaf" == $bTree) {
					$menu["c_$aKey"]["c_$aKey"]["m_$bKey"]
				    = array($data[$bKey]['name'], $data[$bKey]['url']);
				}else{
					foreach ($bTree as $cKey => $cTree){
						$menu["c_$aKey"]["c_$bKey"]["m_$cKey"]
						= array($data[$cKey]['name'], $data[$cKey]['url']);
					}
				}
			}
		}
		return $menu;
	}
	
	function generateNav(){
		$ret = array();
		
		$data = $this->getChildren($this->_rootId, false, $recursive=false, $format = "array_detail");
		foreach ($data as &$row){
			$key = "c_" . $row[$this->pk];
			$ret[$key]= $row['name'];
		}
		return $ret;
	}
	
	function generateTitles(){
		$data = $this->getData();
		foreach ($data as &$row){
			$key = "c_" . $row[$this->pk];
			$ret[$key] = $row['name'];
		}
		return $ret;
	}
}
?>